package com.carl.zlds.dao.sqlprovider;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.carl.zlds.pojo.dto.AccountSearchDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;

import java.util.HashMap;

/**
 * @Author: carl
 * @Date: 2020/6/24 2:29 PM
 */
public class GameAccountSqlProvider {
    public String getGameAccountList(HashMap<String, Object> param) {
        Long gameId = (Long) param.get("gameId");
        Integer status = (Integer) param.get("status");
        String limitSql = (String) param.get("limitSql");
        StringBuilder sql = new StringBuilder();
        if (limitSql.isEmpty()) {
            sql.append(" select count(ga.id) ");
        } else {
            sql.append(" select ga.*, gad.content as content ");
        }
        sql.append(" from game_account ga, game_account_detail gad ");
        sql.append(" where ga.weight >= 0 and ga.id = gad.game_account_id ");
        if (gameId > 0) {
            sql.append(" and ga.game_id = #{gameId} ");
        }
        if (status > 0) {
            sql.append(" and ga.status = #{status} ");
        }
        sql.append(" order by ga.update_time desc ");

        System.out.println(sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY));
        return sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY);
    }

    public String getAccountSearch(HashMap<String, Object> param) {
        AccountSearchDTO dto = (AccountSearchDTO) param.get("dto");
        String limitSql = (String) param.get("limitSql");
        StringBuilder sql = new StringBuilder();
        if (limitSql.isEmpty()) {
            sql.append(" select count(ga.id) ");
        } else {
            sql.append(" select ga.*, gad.content as content ");
        }
        sql.append(" from game_account ga, game_account_detail gad ");
        sql.append(" where ga.weight >= 0 and ga.id = gad.game_account_id ");
        if (dto.getGameId() > 0) {
            sql.append(" and ga.game_id = #{dto.gameId} ");
        }
        if (Strings.isNotBlank(dto.getTitle())) {
            sql.append(" and ga.title like concat('%', #{dto.title},'%') ");
        }
        if (ObjectUtils.isNotEmpty(dto.getGameTagInfoList())) {

            sql.append(" and locate(#{dto.gameTagInfoList}, game_tag_info_list) > 0 ");
        }
        if (dto.getMinPrice() > 0) {
            sql.append(" and ga.price > #{dto.minPrice} ");
        }
        if (dto.getMaxPrice() > 0) {
            sql.append(" and ga.price > #{dto.maxPrice} ");
        }
        if (dto.getMinS1() > 0) {
            sql.append(" and ga.s1 > #{dto.minS1} ");
        }
        if (dto.getMaxS1() > 0) {
            sql.append(" and ga.s1 > #{dto.maxS1} ");
        }
        if (dto.getMinS2() > 0) {
            sql.append(" and ga.s2 > #{dto.minS2} ");
        }
        if (dto.getMaxS2() > 0) {
            sql.append(" and ga.s2 > #{dto.maxS2} ");
        }
        if (dto.getMinLaunchTime() != null) {
            sql.append(" and ga.launch_time > #{dto.minLaunchTime} ");
        }
        if (dto.getMaxLaunchTime() != null) {
            sql.append(" and ga.launch_time M #{dto.maxLaunchTime} ");
        }
        sql.append(" order by ga.update_time desc ");
        if (dto.getHotSort() == 0) {
            sql.append(" , ga.hot asc ");
        } else if (dto.getHotSort() == 1) {
            sql.append(" , ga.hot desc ");
        }

        if (dto.getPriceSort() == 0) {
            sql.append(" , ga.price asc ");
        } else if (dto.getPriceSort() == 1) {
            sql.append(" , ga.price desc ");
        }

        if (dto.getS1Sort() == 0) {
            sql.append(" , ga.s1 asc ");
        } else if (dto.getS1Sort() == 1) {
            sql.append(" , ga.s1 desc ");
        }

        if (dto.getS2Sort() == 1) {
            sql.append(" , ga.s2 asc ");
        } else if (dto.getS2Sort() == 2) {
            sql.append(" , ga.s2 desc ");
        }

        System.out.println(sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY));
        return sql.toString() + StringUtils.defaultString(limitSql, StringUtils.EMPTY);
    }
}
